import 'package:aliplayer_widget/utils/snack_bar_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hktk_app/dao/industry_dao.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/view_util.dart';
import 'package:hktk_app/widget/mine/feedback_widget.dart';
import 'package:hktk_app/widget/mine/problem_type_widget.dart';
import 'package:hktk_app/widget/page_title_widget.dart';

/// 意见反馈页面
class OpinionPage extends StatefulWidget {
  const OpinionPage({super.key});

  @override
  State<OpinionPage> createState() => _OpinionPageState();
}

class _OpinionPageState extends State<OpinionPage> {
  late BuildContext _context;

  /// 问题类型，对应下标
  late int _typeIndex;

  /// 联系方法输入框控制器
  late TextEditingController _phoneTextController;

  /// 反馈描述输入框控制器
  late TextEditingController _feedbackTextController;

  @override
  void initState() {
    super.initState();
    _typeIndex = 0;
    _phoneTextController = TextEditingController();
    _feedbackTextController = TextEditingController();
  }

  @override
  void dispose() {
    super.dispose();
    _phoneTextController.dispose();
    _feedbackTextController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    _context = context;
    return Scaffold(
      backgroundColor: colorWhite2,
      body: Column(
        children: [
          PageTitleWidget(name: "意见反馈", pageContext: _context),
          Expanded(child: _content()),
        ],
      ),
    );
  }

  /// 按钮组件
  _btnItem() {
    return GestureDetector(
      onTap: _onTapFunTj,
      child: Container(
        width: 1.sw - 30 - 30,
        padding: EdgeInsets.symmetric(vertical: 10),
        decoration: BoxDecoration(
          color: colorBlue1,
          borderRadius: BorderRadius.circular(49),
        ),
        child: Center(
          child: Text(
            "提交",
            style: FontUtil.fontWhile1(22),
          ),
        ),
      ),
    );
  }

  /// 提交按钮点击
  _onTapFunTj() {
    final String questionType;
    if (_typeIndex == 0) {
      questionType = "bug";
    } else {
      questionType = "normal";
    }

    IndustryDao.submitSuggestion({
      "questionType": questionType,
      "questionDescription": _feedbackTextController.text,
      "contactInfo": _phoneTextController.text
    }).then((res) {
      if (mounted) {
        SnackBarUtil.success(context, "提交成功");
      }
    });
  }

  /// 内容组件
  _content() {
    var of = MediaQuery.of(_context);
    double width = of.size.width;
    double height = of.size.height - of.padding.top - 88;
    return SingleChildScrollView(
      child: SizedBox(
        width: width,
        height: height,
        child: Stack(
          children: [
            _listItems(),
            Positioned(
              left: 30,
              bottom: 30,
              child: _btnItem(),
            ),
          ],
        ),
      ),
    );
  }

  /// 主体内容
  _listItems() {
    double bottom = MediaQuery.of(_context).padding.bottom;
    double listBottom = bottom + 30 + 88 + 24;
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 24),
      child: MediaQuery.removePadding(
        removeTop: true,
        context: _context,
        child: ListView(
          children: [
            hiSpace(height: 24),
            ProblemTypeWidget(index: _typeIndex, onTapFun: _onTapFunWtlx),
            hiSpace(height: 20),
            FeedbackWidget(textEditingController: _feedbackTextController),
            hiSpace(height: 20),
            _contactItem(),
            hiSpace(height: listBottom),
          ],
        ),
      ),
    );
  }

  /// 问题类型点击响应方法
  void _onTapFunWtlx(int value) {
    setState(() {
      _typeIndex = value;
    });
  }

  /// 联系方法
  _contactItem() {
    return Container(
      height: 170,
      padding: EdgeInsets.symmetric(vertical: 30, horizontal: 24),
      decoration: BoxDecoration(
        color: colorWhite1,
        borderRadius: BorderRadius.circular(16),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            "联系方式（选填）",
            style: FontUtil.fontBlack1(26, fontWeight: FontWeight.w500),
          ),
          _phoneTextFiled(),
        ],
      ),
    );
  }

  /// 联系方式输入框
  _phoneTextFiled() {
    return SizedBox(
      height: 42,
      child: TextField(
        controller: _phoneTextController,
        // 设置光标颜色
        cursorColor: colorBlue1,
        // 光标高度
        cursorHeight: 42,
        // 样式
        style: FontUtil.fontGrey5(30),
        // 装饰
        decoration: InputDecoration(
          // 去掉textField自定的高度
          isCollapsed: true,
          // 内容边距
          contentPadding: EdgeInsets.only(bottom: 3),
          // 去除边框
          border: InputBorder.none,
          // 提示语句
          hintText: "请输入你的手机号",
          // 提示语句样式
          hintStyle: FontUtil.fontGrey5(20),
        ),
      ),
    );
  }
}
